package com.open.capacity.notice.service.impl;

import java.util.List;
import java.util.Objects;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.open.capacity.common.utils.BatchUtil;
import com.open.capacity.common.utils.EntityUtils;
import com.open.capacity.notice.dmo.MessageRececiverDetail;
import com.open.capacity.notice.dmo.MessageRececiverGroup;
import com.open.capacity.notice.dto.MessageRececiverGroupDto;
import com.open.capacity.notice.mapper.MessageRececiverGroupMapper;
import com.open.capacity.notice.service.MessageRececiverDetailService;
import com.open.capacity.notice.service.MessageRececiverGroupService;
import com.open.capacity.notice.vo.MessageRececiverGroupVO;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 服务实现类
 * </p>
 * 
 * @since 2024-05-07
 */
@Service
@RequiredArgsConstructor
public class MessageRececiverGroupServiceImpl extends ServiceImpl<MessageRececiverGroupMapper, MessageRececiverGroup>
		implements MessageRececiverGroupService {

	private final MessageRececiverDetailService messageRececiverDetailService;

	@Override
	@Transactional
	public MessageRececiverGroup add(MessageRececiverGroupDto messageRececiverGroupDto) {
		MessageRececiverGroup messageRececiverGroup = EntityUtils.toObj(messageRececiverGroupDto,
				MessageRececiverGroup::new);
		List<MessageRececiverDetail> messageRececiverDetails = EntityUtils
				.toList(messageRececiverGroupDto.getRecevivers(), recevier -> {
					recevier.setRececiverGroupId(messageRececiverGroup.getId());
					return recevier;
				});
		messageRececiverGroup.setRececiverCount(messageRececiverDetails.size());
		this.save(messageRececiverGroup);
		messageRececiverDetailService.saveBatch(messageRececiverDetails, 100);
		return messageRececiverGroup;
	}

	@Override
	@Transactional
	public MessageRececiverGroup edit(MessageRececiverGroupDto messageRececiverGroupDto) {
		MessageRececiverGroup messageRececiverGroup = EntityUtils.toObj(messageRececiverGroupDto,
				MessageRececiverGroup::new);
		List<MessageRececiverDetail> messageRececiverDetails = messageRececiverGroupDto.getRecevivers();
		messageRececiverGroup.setRececiverCount(messageRececiverDetails.size());
		this.saveOrUpdate(messageRececiverGroup);
		List<String> delList = messageRececiverGroupDto.getDelLists();
		if (CollectionUtils.isNotEmpty(delList)) {
			BatchUtil.batchHandle(delList.size(), 200, (offset, size) -> {
				int toIndex = Math.min(offset + size, delList.size());
				List<String> splitTasks = delList.subList(offset, toIndex);
				messageRececiverDetailService.remove(
						Wrappers.<MessageRececiverDetail>lambdaUpdate().in(MessageRececiverDetail::getId, splitTasks));
			});
		}
		messageRececiverDetailService.saveOrUpdateBatch(messageRececiverDetails, 100);
		return messageRececiverGroup;
	}

	@Override
	public MessageRececiverGroupVO queryById(String id) {
		MessageRececiverGroup messageRececiverGroup = this.getById(id);
		List<MessageRececiverDetail> list = messageRececiverDetailService.list(
				Wrappers.<MessageRececiverDetail>lambdaQuery().eq(MessageRececiverDetail::getRececiverGroupId, id));
		MessageRececiverGroupVO messageRececiverGroupVO = EntityUtils.toObj(messageRececiverGroup,
				MessageRececiverGroupVO::new);
		if (!Objects.isNull(messageRececiverGroup)) {
			messageRececiverGroupVO.setRecevivers(list);
		}
		return messageRececiverGroupVO;
	}

	@Override
	@Transactional
	public boolean delete(String id) {
		messageRececiverDetailService.remove(
				Wrappers.<MessageRececiverDetail>lambdaQuery().eq(MessageRececiverDetail::getRececiverGroupId, id));
		return this.removeById(id);
	}

}
